package com.wanlong.admin.controller.system;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.wanlong.common.result.PageResult;
import com.wanlong.common.result.VueResult;
import com.wanlong.rpc.service.SysMemberManagerService;
import com.wanlong.sys.model.SysPrivilege;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 万龙
 * @name
 * @project_name wanlong
 * @package_name com.wanlong.admin.controller.system
 * @description
 * @date 2019/2/25
 */
@Controller
@CrossOrigin(origins = "*", maxAge = 3600)//跨域请求
@RequestMapping(value = "/admin/privilege")
public class PrivilegeController {

    private static ObjectMapper MAPPER = new ObjectMapper();

    @Autowired
    public SysMemberManagerService sysMemberManagerService;

    @RequestMapping(value = "/privilegeList" , method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public VueResult privilegeList(@RequestParam("name")String name, @RequestParam("page")Integer page, @RequestParam("limit")Integer limit){
        PageInfo<SysPrivilege> sysPrivilegeList = sysMemberManagerService.getPageSysPrivilegeListByName(name,page,limit);
        List<SysPrivilege> ul = sysPrivilegeList.getList();
        PageResult pageResult = new PageResult();
        pageResult.setItems(ul);
        pageResult.setTotal(sysPrivilegeList.getTotal());

//        Map<String ,Object> map = new HashMap<>();
//        map.put("items",ul);
//        map.put("total",sysPrivilegeList.getTotal());
//        String result="";
//        try {
//            result= MAPPER.writeValueAsString(map);
//        } catch (JsonProcessingException e) {
//            e.printStackTrace();
//        }
        return VueResult.success(pageResult);
    }

    @RequestMapping(value = "/createPrivilege" , method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public VueResult createPrivilege(@RequestBody SysPrivilege sysPrivilege){

        String result=sysMemberManagerService.insertSysPrivilege(sysPrivilege)+"";
        return VueResult.success(result);
    }

    @RequestMapping(value = "/updatePrivilege" , method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public VueResult updatePrivilege(@RequestBody SysPrivilege sysPrivilege){
        sysPrivilege.setGmtModified(new Date());
        String result=sysMemberManagerService.updateSysPrivilege(sysPrivilege)+"";

        return VueResult.success();
    }

    @RequestMapping(value = "/deletePrivilege" , method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public VueResult deletePrivilege(@RequestBody SysPrivilege sysPrivilege){
        sysPrivilege.setGmtModified(new Date());

        String result=sysMemberManagerService.deleteSysPrivilege(sysPrivilege)+"";

        return VueResult.success();
    }
}
